<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class BalanceOrder extends \app\BaseModel
{
    protected $name = "massage_service_balance_order_list";
    protected $append = ["nick_name"];
    public function getNickNameAttr($value, $data)
    {
        if(!empty($data["user_id"])) {
            $user_model = new User();
            return $user_model->where(["id" => $data["user_id"]])->value("nickName");
        }
    }
    public function dataAdd($data)
    {
        $data["status"] = 1;
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page = 10)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function orderResult($order_code, $transaction_id)
    {
        $order = $this->dataInfo(["order_code" => $order_code, "transaction_id" => ""]);
        if(!empty($order) && !empty($transaction_id)) {
            $user_model = new User();
            $water_model = new BalanceWater();
            $user = $user_model->dataInfo(["id" => $order["user_id"]]);
            \think\facade\Db::startTrans();
            $update = ["transaction_id" => $transaction_id, "status" => 2, "pay_time" => time(), "now_balance" => $order["true_price"] + $user["balance"]];
            $res = $this->dataUpdate(["id" => $order["id"], "transaction_id" => ""], $update);
            if($res == 0) {
                \think\facade\Db::rollback();
            }
            $is_add = $order["type"] == 3 ? 0 : 1;
            $type = $is_add == 1 ? 1 : 5;
            $res = $water_model->updateUserBalance($order, $type, $is_add);
            if($res == 0) {
                \think\facade\Db::rollback();
            }
            $integral_model = new Integral();
            $coach_model = new Coach();
            $integral_record = $integral_model->dataInfo(["order_id" => $order["id"], "type" => 0, "status" => -1]);
            if(!empty($integral_record)) {
                $integral_model->dataUpdate(["id" => $integral_record["id"]], ["status" => 1]);
                $integral = $integral_record["integral"];
                $coach_model->where(["id" => $order["coach_id"]])->update(["integral" => \think\facade\Db::Raw("integral+" . $integral)]);
            }
            $comm_model = new Commission();
            $comm_data = $comm_model->dataInfo(["order_id" => $order["id"], "status" => -1, "type" => 7]);
            if(!empty($comm_data)) {
                $integral_model->dataUpdate(["order_id" => $order["id"], "type" => 1, "status" => -1], ["status" => 1]);
                $comm_model->dataUpdate(["id" => $comm_data["id"]], ["status" => 1]);
            }
            if($is_add == 1) {
                $level_model = new \app\member\model\Level();
                $level_model->levelUp($order, 4);
            }
            $member_model = new \app\adapay\model\Member();
            $member_model->adaPayBalanceMyself($order);
            \think\facade\Db::commit();
            \think\facade\Db::startTrans();
            if(!empty($comm_data)) {
                $comm_model->dataUpdate(["id" => $comm_data["id"]], ["status" => 2, "cash_time" => time()]);
                $cash = $comm_data["cash"];
                $coach_model->where(["id" => $order["coach_id"]])->update(["balance_cash" => \think\facade\Db::Raw("balance_cash+" . $cash)]);
                $water_model = new CoachWater();
                $res = $water_model->updateCash($comm_data["uniacid"], $order["coach_id"], $cash, 1, 1);
                if($res == 0) {
                    \think\facade\Db::rollback();
                }
            }
            \think\facade\Db::commit();
        }
        return true;
    }
}

?>